import { getArticleDetailApi, getArticleListApi } from '@/api/article';
import { AppDispatch } from '..';
import { ArticleActionType } from '../reducer/article';

//保存频道id
export const saveChannelIdAction = (id: string): ArticleActionType => {
  return { type: 'article/channelId', payload: id };
};
//获取文章列表
export const getArticleListAction = (params: {
  channel_id: number;
  timestamp: number;
}) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArticleListApi(params);
    // console.log(res);
    dispatch({
      type: 'article/saveList',
      payload: { ...res.data, channel_id: params.channel_id },
    });
  };
};
//更新文章列表(需要累加数据)
export const updateArticleListAction = (params: {
  channel_id: number;
  timestamp: number;
}) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArticleListApi(params);
    // console.log(res);
    dispatch({
      type: 'article/updateList',
      payload: { ...res.data, channel_id: params.channel_id },
    });
  };
};

//获取文章详情
export const getArticleDetailAction = (id: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArticleDetailApi(id);
    console.log(res);
    dispatch({ type: 'article/detail', payload: res.data });
  };
};
